﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;
using System.IO;
namespace ocruit
{
    [Serializable]
    public class styleset
    {
         List<Letter> letters_ = new List<Letter>() ;
        
         
        public  const int letterCount = 26;
        
        public IEnumerable<Letter> letters
        {
            get
            {
                for (int i = 0; i < letters_.Count; i++ )
                    yield return letters_[i];
            }
            
        }
        
        public  styleset()
        {
            if (letters_.Count == 0)
            for (int i = 0; i < letterCount; i++)
                letters_.Add(new Letter());
            this.save();
        }
        
        public static styleset Load()
        {
            if (File.Exists("data.bin"))
            {
                FileStream file = new FileStream("data.bin", FileMode.Open, FileAccess.ReadWrite);
                BinaryFormatter bin = new BinaryFormatter();
                styleset st = (styleset)bin.Deserialize(file);
                file.Close();
                return st;
            }
            else
                return new styleset();
        }
        public   void save()
        {
            FileStream file; 
            if(!File.Exists("data.bin"))
            {
                file = new FileStream("data.bin", FileMode.Create, FileAccess.ReadWrite);
            }
            else
            {
             file = new FileStream("data.bin", FileMode.Open, FileAccess.ReadWrite);
            }
            if (this.letters_.Count != 0)
            {
                BinaryFormatter bin = new BinaryFormatter();
                bin.Serialize(file, this);
                file.Close();
            }
            return;
        }
        public styleset(SerializationInfo info , StreamingContext context)
        {
            this.letters_ = (List<Letter>)info.GetValue("letters" , typeof(List<Letter>));
            
        }
        public void GetObjectData(SerializationInfo info , StreamingContext context)
        {
            info.AddValue("letters", this.letters_);
        }
        public void setLetterContent(int index ,string base64)
        {
            letters_[index].addInstance(base64);
        }
        public Letter getLetter(int index)
        {
            if (letters_.Count != 0)
                return letters_[index];
            else
                return null; 
        }
        public void addLetter(Letter lt)
        {
            letters_.Add(lt);
        }
        public void addLetter(string base64)
        {
            if (letters_.Count != 0)
                letters_[letters_.Count].addInstance(base64);
            else
            {
                letters_.Add(new Letter());
                letters_[0].addInstance(base64);
            }
        }
        public void addLetter(string base64 , int index)
        {
            if (letters_.Count != 0 && index <= letters_.Count)
                letters_[index].addInstance(base64);
            else
                return;
        }
        public void removeLetter(Letter lt)
        {
            letters_.Remove(lt);
        }
        public void RemoveLetterAt(int index)
        {
            letters_.RemoveAt(index);
        }
        public void removeAll()
        {
            letters_.Clear();
        }
    }
}
